//
//  CustomCircleView.m
//  BMS
//
//  Created by Guang Li on 2021/4/28.
//

#import "CustomCircleView.h"

#define LineWidth 8

@interface CustomCircleView ()

@property (nonatomic, assign) CGFloat vWidth;
@property (nonatomic, strong) CAShapeLayer *shapeLayer;
@property (nonatomic, strong) CADisplayLink *displayLink;
@property (nonatomic, strong) UIBezierPath *bPath;

@end

@implementation CustomCircleView
{
    CGFloat _startAngle; // 开始的角度
    NSInteger _startRate;
}

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        _startAngle = -90; // 从圆的最顶部开始
        _startRate = 0;
        _vWidth = frame.size.width;
        _bPath = [UIBezierPath bezierPath];
        // 先画一个底部的圆
        [self configBgCircle];
        // 配置CAShapeLayer
        [self configShapeLayer];
        // 配置CADisplayLink
        [self configDisplayLink];
    }
    return self;
}

#pragma mark - event response

- (void)drawCircle {
    if (_startRate >= _rate) {
        _bPath = [UIBezierPath bezierPath];
        _displayLink.paused = YES;
        return;
    }
    _startRate ++;
    [_bPath addArcWithCenter:CGPointMake(self.frame.size.width * 0.5, self.frame.size.height * 0.5) radius:_vWidth * 0.5 startAngle:(M_PI / 180.0) * _startAngle endAngle:(M_PI / 180.0) * (_startAngle + 3.6) clockwise:YES];
    _shapeLayer.path = _bPath.CGPath;
    _startAngle += 3.6;
}

#pragma mark - public methods

- (void)startAnimation {
//    if (_displayLink.paused == YES) {
        _startAngle = -235;
//        _startRate = 0;
//        _displayLink.paused = NO;
//    }

    if (self.rate>100) {
        self.rate = 100;
    }else if (self.rate<0) {
        self.rate = 0;
    }
    CGFloat bl = 0.8;
    if (self.rate <= 28) {
        bl = 0.75;
    }else if (self.rate <=32) {
        bl = 0.78;
    }
    NSInteger r = self.rate * bl;
    _bPath = [UIBezierPath bezierPath];
    [_bPath addArcWithCenter:CGPointMake(self.frame.size.width * 0.5, self.frame.size.height * 0.5) radius:_vWidth * 0.5 startAngle:(M_PI / 180.0) * _startAngle endAngle:(M_PI / 180.0) * (_startAngle + r * 3.6) clockwise:YES];
    _shapeLayer.strokeColor = self.rate > 20 ? SUBJECT_COLOR.CGColor : [UIColor redColor].CGColor;
    _shapeLayer.path = _bPath.CGPath;
}

#pragma mark 底下灰色的圆
- (void)configBgCircle {
    NSInteger startAngle = -235;
    UIBezierPath *bPath = [UIBezierPath bezierPathWithArcCenter:CGPointMake(self.frame.size.width * 0.5, self.frame.size.height * 0.5) radius:_vWidth * 0.5 startAngle:(M_PI / 180.0) * startAngle endAngle:(M_PI / 180.0) * (startAngle + 80 * 3.6) clockwise:YES];
    CAShapeLayer *shaperLayer = [CAShapeLayer layer];
    shaperLayer.fillColor = [UIColor clearColor].CGColor;
    shaperLayer.strokeColor = RGB16(0xEDEDED).CGColor;
    shaperLayer.lineWidth = LineWidth;
    shaperLayer.path = bPath.CGPath;
    shaperLayer.lineCap = kCALineCapRound;
    [self.layer addSublayer:shaperLayer];
}

#pragma mark 配置CAShaperLayer
- (void)configShapeLayer {
    _shapeLayer = [CAShapeLayer layer];
    _shapeLayer.strokeColor = SUBJECT_COLOR.CGColor;
    _shapeLayer.fillColor = [UIColor clearColor].CGColor;
    _shapeLayer.lineWidth = LineWidth;
    _shapeLayer.lineCap = kCALineCapRound;
    [self.layer addSublayer:_shapeLayer];
}

#pragma mark 配置CADisplayLink
- (void)configDisplayLink {
    _displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(drawCircle)];
    [_displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
    _displayLink.paused = YES; // 默认暂停
}


@end
